package com.wyt.cloud.utils.query;

import com.google.common.collect.Lists;

import java.lang.reflect.Field;
import java.util.Arrays;
import java.util.List;

public class ClazzFieldsUtil {


    /**
     * 递归获取单管类所有父类的字段
     */
    public static List<Field> getAllFields(Class targetClazz) {
        return getAllFields(targetClazz, null);
    }

    public static List<Field> getAllFields(Class targetClazz, Class excludeClass) {
        List<Field> list = Lists.newArrayList();
        list.addAll(Arrays.asList(targetClazz.getDeclaredFields()));

        while (targetClazz.getSuperclass() != Object.class) {
            targetClazz = targetClazz.getSuperclass();

            if (excludeClass != null && targetClazz.getSimpleName().equals(excludeClass.getSimpleName())) {
                break;
            }
            list.addAll(Arrays.asList(targetClazz.getDeclaredFields()));
        }
        return list;
    }
}
